﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.PromptReport;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.FirstPregnancyTestPromptes
{
    public class FirstPregnancyTestPromptQueryHandler : IRequestHandler<FirstPregnancyTestPromptQueryCommand, Result>
    {
        private readonly IBaseRepository<FirstPregnancyTestPrompt> _firstPregnancyTestPromptRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<Breedingsetting> _breedingsettingRepository;
        private readonly IBaseRepository<PregnancyTest> _pregnancyTestRepository;

        public FirstPregnancyTestPromptQueryHandler(IBaseRepository<FirstPregnancyTestPrompt> firstPregnancyTestPromptRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<Breedingsetting> breedingsettingRepository, IBaseRepository<PregnancyTest> pregnancyTestRepository)
        {
            _firstPregnancyTestPromptRepository = firstPregnancyTestPromptRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _sheepArchivesRepository = sheepArchivesRepository;
            _breedingsettingRepository = breedingsettingRepository;
            _pregnancyTestRepository = pregnancyTestRepository;
        }
        /// <summary>
        /// 首次妊检
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FirstPregnancyTestPromptQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            int day = 0;
            try
            {
                var firstPregnancyTestPrompt = await (from a in await _sheepArchivesRepository.GetAll()
                                                      join c in await _cottageRepository.GetAll() on a.CottageId equals c.CottageId
                                                      join f in await _fieldRepository.GetAll() on a.FieldId equals f.FieldId
                                                      join b in await _pregnancyTestRepository.GetAll() on a.RecordAuricle equals b.EarBugles
                                                      join d in await _firstPregnancyTestPromptRepository.GetAll() on a.RecordAuricle equals d.SheepNumber
                                                      join e in await _breedingsettingRepository.GetAll() on a.RecordAuricle equals e.RecordAuricle
                                                      where  b.CreatedOwnerId != "" && b.CreatedTime != null
                                                      select new
                                                      {
                                                           a.RecordAuricle,//耳号
                                                           c.CottageId,//栋舍id
                                                           c.CottageName,//栋舍名称
                                                           f.FieldId,//栏位Id
                                                           f.FieldName,//栏位名称
                                                           d.PreparationType,//类型
                                                           day =Math.Floor((Convert.ToDateTime(b.PregnancyTestDate)-Convert.ToDateTime(e.BreedingsettingTime)).TotalDays)
                                                      }).ToListAsync();
                result.Data = firstPregnancyTestPrompt;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
